package com.higgins.eventtracking.client.health;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.TabBar;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.higgins.eventtracking.client.health.action.ActionTab;
import com.higgins.eventtracking.client.health.components.GenericStatsTab;
import com.higgins.eventtracking.client.health.components.GenericTabInterface;
import com.higgins.eventtracking.client.health.components.GenericStatsTabParent;
import com.higgins.eventtracking.client.health.fundamentals.Fundamentals;
import com.higgins.eventtracking.client.health.fundamentals.FundamentalsChartPanel;
import com.higgins.eventtracking.client.health.record.RecordFixedRollingEntryTabs;
import com.higgins.eventtracking.client.health.record.fixed.RecordFixedTab;
import com.higgins.eventtracking.client.health.secondary.Secondary;
import com.higgins.eventtracking.client.health.settings.SettingsTab;
import com.higgins.eventtracking.client.health.toplevel.TopLevelTab;

public class Health implements EntryPoint, GenericStatsTabParent	{
	private List<GenericTabInterface> listOfTabs = new ArrayList<GenericTabInterface>();
	private TabPanel tabPanel = new TabPanel();
	private PopupQuestionBox popupBox = new PopupQuestionBox();

	public Health()	{
		super();
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void onModuleLoad() {
		popupBox.checkForQuestions();
		int tabIndex = 0;
		tabPanel.setSize("200em","100em");

		final TopLevelTab topLevelTab = new TopLevelTab(this);
		topLevelTab.setTabTitle("Top Level");
		topLevelTab.setTabIndex(tabIndex++);
		tabPanel.add(topLevelTab, topLevelTab.getTabTitle());
		topLevelTab.update();
		listOfTabs.add(topLevelTab);
		
		final Fundamentals fundamentalsTab = new Fundamentals(this);
		fundamentalsTab.setTabTitle("Fundamentals");
		fundamentalsTab.setTabIndex(tabIndex++);
		fundamentalsTab.addChartPanel(new FundamentalsChartPanel());
		tabPanel.add(fundamentalsTab, fundamentalsTab.getTabTitle());
		fundamentalsTab.update();
		listOfTabs.add(fundamentalsTab);

		final Secondary secondary = new Secondary(this);
		secondary.setTabTitle("Secondary");
		secondary.setTabIndex(tabIndex++);
		tabPanel.add(secondary, secondary.getTabTitle());
		secondary.update();
		listOfTabs.add(secondary);

		final ActionTab actionTab = new ActionTab();
		actionTab.setTabTitle("Action");
		actionTab.setTabIndex(tabIndex++);
		tabPanel.add(actionTab, actionTab.getTabTitle());
		listOfTabs.add(actionTab);

		final RecordFixedRollingEntryTabs recordTab = new RecordFixedRollingEntryTabs();
		recordTab.setTabTitle("Record");
		recordTab.setTabIndex(tabIndex++);
		tabPanel.add(recordTab, recordTab.getTabTitle());
		listOfTabs.add(recordTab);
		
		final SettingsTab settingsTab = new SettingsTab();
		settingsTab.setTabTitle("Settings");
		settingsTab.setTabIndex(tabIndex++);
		tabPanel.add(settingsTab, settingsTab.getTabTitle());
		listOfTabs.add(settingsTab);
		
		tabPanel.selectTab(0);
		RootLayoutPanel.get().add(tabPanel);
		tabPanel.addSelectionHandler(new SelectionHandler<Integer>() {
            @Override
            public void onSelection(SelectionEvent<Integer> integerSelectionEvent) {
            	int selectedIndex = integerSelectionEvent.getSelectedItem();
            	GenericTabInterface tab = listOfTabs.get(selectedIndex);
            	if (tab!=null)	{
            		tab.update();
            	}
            }
        });
	}

	@Override
	public void updateTabTitle(Integer tabIndex, String title, Boolean alert) {
		TabBar tabBar = tabPanel.getTabBar();
		tabBar.setTabHTML(tabIndex, alert?"<div class=\"highlightAlertLabel\" style=\"width: "+ (title.length()*9) +"px;\">" + title + "</div>":title);
	}
}
